<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Query\GoodsQuery;
use Log;
use App\Http\Controllers\Controller;
use App\Http\Query\SetQuery;
use App\Http\Query\CategoryQuery;

class SetController extends Controller
{

    /**
     * セット品マスタ
     * 一覧表示
     */
    public function index(Request $request)
    {
       try{
            $setMasterQuery = new SetQuery();
            // $setMasterInfo = $setMasterQuery->viewSetGoodsMaster();
            $setMasterInfo = $setMasterQuery->getSets();
            $CategoryMaster = new CategoryQuery();
            $categoryInfo = $CategoryMaster->getCategoryInfoByLoginId();

            $allInfo = [
                $categoryInfo,
                $setMasterInfo
            ];

            return view('admin.set', array(
                'allInfo' => $allInfo
            ));
          }catch (\Exception $e){
            \Log::error($e->getFile()." Line:".$e->getLine()." Msg:".$e->getMessage());
        }
    }

    /**
     * セット品マスタ
     * goodsMsaterデータを取る
     */
    public function indexJson(Request $request)
    {
      try{
            $goodsCode = $request->input('searchKey');

            // goodsMsaterデータを取る
            $goodsQuery = new GoodsQuery();
            $goodsInfo = $goodsQuery->getGoodsJson($goodsCode);

            // Jsonデータを返す
            exit(json_encode($goodsInfo, JSON_UNESCAPED_UNICODE));
        }catch (\Exception $e){
            \Log::error($e->getFile()." Line:".$e->getLine()." Msg:".$e->getMessage());
        }
    }

    /**
     * セット品マスタ
     * セット品情報を表示
     */
    public function showAddSetView(Request $request)
    {
       try{
            $set_id = $request->input('set_id');

            $setMasterQuery = new SetQuery();

            $CategoryMaster = new CategoryQuery();

            $categoryInfo = $CategoryMaster->getCategoryInfoByLoginId();
            $setMasterInfo = new SetQuery();
            $setGoods = array();
            if ($set_id) {
                $setMasterInfo = $setMasterQuery->getAllSetsInfo($set_id)[0];
                $setGoods = $setMasterQuery->viewSetGoodsMaster($set_id);
                $allInfo = [
                    $categoryInfo,
                    $setMasterInfo
                ];
            } else {
                $allInfo = [
                    $categoryInfo,
                    $setMasterInfo
                ];
            }

            return view('admin.set_add', array(
                'allInfo' => $allInfo,
                'setMasterInfo' => $setMasterInfo,
                'setGoods' => $setGoods
            ));
          }catch (\Exception $e){
            \Log::error($e->getFile()." Line:".$e->getLine()." Msg:".$e->getMessage());
        }
    }

    /**
     * セット品マスタ
     * セット品情報を登録する
     */
    public function addSetGoodsMaster(Request $request)
    {
       try{
            $setMasterQuery = new SetQuery();
            $set_id = $request->input('set_id');
            $setName = $request->input('set_name');
            $start_date = $request->input('start_date');
            $end_date = $request->input('end_date');
            $rec_num = $request->input('rec_num');
            $flag = 1;
            $set_id = $setMasterQuery->insertSetMaster($setName, $start_date, $end_date);
            $setMasterQuery->deleteSetGoods($set_id);
            for ($i = 0; $i < $rec_num; $i ++) {
                $goodsId = $request->input('goodsId_' . $i);
                if (empty($goodsId)) {
                    continue;
                }
                ;
                $setMasterQuery->insSetGoodsMaster($set_id, $goodsId);
            }

            if ($flag) {
                return redirect('/admin/set')->with('success', '追加成功');
            }
          }catch (\Exception $e){
             \Log::error($e->getFile()." Line:".$e->getLine()." Msg:".$e->getMessage());
        }
    }

    /**
     * セット品マスタ
     * セット品情報を削除する
     */
    public function delAddSetView(Request $request)
    {
       try{
            $goodsId = $request->input('goods_id');

            $setMasterQuery = new SetQuery();

            $flag = $setMasterQuery->delSetMaster($goodsId);

            if ($flag) {
                return redirect('/admin/set')->with('success', '削除成功');
            }
          }catch (\Exception $e){
            \Log::error($e->getFile()." Line:".$e->getLine()." Msg:".$e->getMessage());
        }
    }

    /**
     * セット品マスタ
     * セット品情報を修正する
     */
    public function updateSetView(Request $request)
    {
       try{
            $setMasterQuery = new SetQuery();
            $set_id = $request->input('set_id');
            $setName = $request->input('set_name');
            $start_date = $request->input('start_date');
            $end_date = $request->input('end_date');
            $rec_num = $request->input('rec_num');

            if (! empty($set_id)) {
                $flag = $setMasterQuery->updateSetMaster($set_id, $setName, $start_date, $end_date);
                $setMasterQuery->deleteSetGoods($set_id);
                for ($i = 0; $i < $rec_num; $i ++) {
                    $goodsId = $request->input('goodsId_' . $i);
                    if (empty($goodsId)) {
                        continue;
                    }
                    ;
                    $setMasterQuery->insSetGoodsMaster($set_id, $goodsId);
                }
            }
            if ($flag) {
                return redirect('/admin/set')->with('success', '修正成功');
            }
          }catch (\Exception $e){
            \Log::error($e->getFile()." Line:".$e->getLine()." Msg:".$e->getMessage());
        }
    }
}